package com.example.demo.controller;

import com.example.demo.common.ResultBody;
import com.example.demo.common.response.BaseServiceResponse;
import com.example.demo.model.Product;
import com.example.demo.model.Sample;
import com.example.demo.param.ProductParamVo;
import com.example.demo.param.SampleParamVo;
import com.example.demo.service.SampleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/sample")
public class SampleController {

    @Resource
    SampleService sampleService;

    @RequestMapping(value = "/getAllSample", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public BaseServiceResponse<?> getAllSample() {
        try {
            List<Sample> selectList=sampleService.getAllSample();
            List<Sample> resList=new ArrayList<Sample>();
            if(selectList.size()>2) {
             for(int i=0;i<2;i++) {
                 int a=1 + (int)(Math.random()*selectList.size());
                 resList.add(selectList.get(a));
             }
            } else {
                resList = selectList;
            }
            return new BaseServiceResponse<>(resList,ResultBody.success,"100");
        }catch (Exception e) {
            return null;
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE )
    public BaseServiceResponse<?> getSampleList(@RequestBody SampleParamVo sampleParamVo) {
        try {
            PageHelper.startPage(sampleParamVo.getPageNum(), sampleParamVo.getPageSize());
            List<Sample> list = sampleService.selectAllSample();
            PageInfo pageInfo = new PageInfo(list, sampleParamVo.getPageSize());
            return new BaseServiceResponse<>(pageInfo,ResultBody.success,"success");
        }catch (Exception e){
            return null;
        }
    }

    @RequestMapping(value = "/getSample", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE )
    public BaseServiceResponse<?> getSample(@RequestParam Long id) {
        try {
            Sample sample = sampleService.getSampleById(id);
            return new BaseServiceResponse<>(sample,ResultBody.success,"success");
        }catch (Exception e){
            return null;
        }
    }
}
